#!/usr/bin/env perl
# Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

use IO::Socket::IP;

my $cmd = "vespa-model-inspect -t logtp -t telnet -t replicator filter:ports";

print STDERR "Looking for logserver port ($cmd)\n";

my $vmiout = `$cmd`;
my $host = "";
my $port = "";

if ( $vmiout =~ m{^\s*tcp/(\S+):(\d+)\s*$/} ) {
	$host = $1;
	$port = $2;
	print STDERR "following logserver at '${host}:${port}'\n";
} else {
	die "FAILED finding host and port for logserver replication\n";
}

my $net = new IO::Socket::IP(PeerAddr => "${host}:${port}",
                             Proto    => 'tcp',
                             Timeout  => 10);
if (!$net) {
        die "could not contact logserver at ${host}:${port}\n";
}

print $net "use system.all\r\n";

while (<$net>) {
	if (m/^#/) {
		print STDERR;
	} else {
		print;
	}
}
exit(0);
